package com.gemptc.service.iphone;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import com.gemptc.bean.Travelsbean;
import com.gemptc.bean.UserBean;
import com.gemptc.daoimp.SelectImp;

/**
 * Servlet implementation class KePuTuiJian_Middle
 */
@WebServlet("/KePuTuiJian_Middle")
public class KePuTuiJian_Middle extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public KePuTuiJian_Middle() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter printWriter = response.getWriter();
		System.out.println("信息");
		String Value = request.getParameter("key");
		switch (Integer.parseInt(Value)) {
		case 1:
			System.out.println("jinruxunhuan");
			List<Travelsbean> list2 = new ArrayList<Travelsbean>();
			SelectImp select2 = new SelectImp();
			String t_title = request.getParameter("title");
			list2 = select2.moHuTravelsbean(t_title);
			System.out.println("aaaa"+select2.moHuTravelsbean(t_title));
			System.out.println("aaa"+list2);
			for (int i = 0; i < list2.size(); i++) {
				SelectImp gpd = new SelectImp();
				list2.get(i)
						.setPl_path(gpd.selectPhoto(list2.get(i).getT_id()));
			}
			JSONArray jsonArray2 = JSONArray.fromObject(list2);			
			printWriter.write(jsonArray2.toString());
			printWriter.close();
			break;
		case 2:
			System.out.println("jinruxunhuan");
			List<Travelsbean> list1 = new ArrayList<Travelsbean>();
			SelectImp select = new SelectImp();
			System.out.println("aaa");
			String pageNow = request.getParameter("page");
			list1 = select.getTravelsbean(Integer.parseInt(pageNow));
			for (int i = 0; i < list1.size(); i++) {
				SelectImp gpd = new SelectImp();
				list1.get(i)
						.setPl_path(gpd.selectPhoto(list1.get(i).getT_id()));
			}
			JSONArray jsonArray1 = JSONArray.fromObject(list1);
			printWriter.write(jsonArray1.toString());
			printWriter.close();
			break;
		case 3:
			System.out.println("jinruxunhuan");
			List<UserBean> list10 = new ArrayList<UserBean>();
			SelectImp select1 = new SelectImp();
			System.out.println("aaa");
			String user_id10 = request.getParameter("user_id");
			list10 = select1.userbean(Integer.parseInt(user_id10));
			JSONArray jsonArray10 = JSONArray.fromObject(list10);
			printWriter.write(jsonArray10.toString());
			printWriter.close();
			break;
		case 4:
			// 定义一个list集合 里面是Travelsbean；
			List<Travelsbean> list3 = new ArrayList<Travelsbean>();

			SelectImp selectImp3 = new SelectImp();
			// String user_id="41";
			String user_id = request.getParameter("user_id");
			System.out.println(user_id);
			String photo3 = request.getParameter("page");

			list3 = selectImp3.getTravelsbeanMuLu(Integer.parseInt(photo3),
					Integer.parseInt(user_id));
			for (int i = 0; i < list3.size(); i++) {
				SelectImp gpd = new SelectImp();
				list3.get(i)
						.setPl_path(gpd.selectPhoto(list3.get(i).getT_id()));
			}
			JSONArray jsonArray3 = JSONArray.fromObject(list3);

			printWriter.write(jsonArray3.toString());
			printWriter.close();
			break;
		case 5:
			// 定义一个list集合 里面是Travelsbean；
			List<Travelsbean> travelslist = new ArrayList<Travelsbean>();
			SelectImp selectImp5 = new SelectImp();
			String pageNow1 = request.getParameter("page");
			travelslist = selectImp5.getTravelsbean(Integer.parseInt(pageNow1));
			for (int i = 0; i < travelslist.size(); i++) {
				SelectImp gpd = new SelectImp();
				travelslist.get(i).setPl_path(
						gpd.selectPhoto(travelslist.get(i).getT_id()));
				System.out.println("hujiao"
						+ gpd.selectPhoto(travelslist.get(i).getT_id()));
			}
			JSONArray jsonArray5 = JSONArray.fromObject(travelslist);
			printWriter.write(jsonArray5.toString());
			printWriter.close();
			break;
		case 6:
			System.out.println(6);
			int user_id1 = Integer.parseInt(request.getParameter("user_id"));
			int s_id1 = Integer.parseInt(request.getParameter("t_id"));
			int choice = Integer.parseInt(request.getParameter("choice"));
			System.out.println(user_id1 + s_id1 + choice + "胡蛟龙");
			SelectImp sImp = new SelectImp();
			boolean flag = sImp.Collect(user_id1, s_id1, choice);
			if (flag) {
				printWriter.write("已收藏");
			} else {
				printWriter.write("没有收藏");
			}
			printWriter.close();
			break;
		case 7:
			System.out.println(7);
			int choice1 = Integer.parseInt(request.getParameter("choice"));
			int s_id2 = Integer.parseInt(request.getParameter("t_id"));
			int user_id2 = Integer.parseInt(request.getParameter("user_id"));
			System.out.println(user_id2+"------"+s_id2+"------"+choice1);
			SelectImp sImp1 = new SelectImp();
			boolean flag1 = sImp1.Collect(user_id2, s_id2, choice1);
			System.out.println(flag1);
			if (flag1) {
				printWriter.write("收藏成功");
			} else {
				printWriter.write("收藏失败");
			}
			printWriter.close();
			break;
		case 8:
			System.out.println(8);
			int choice2 = Integer.parseInt(request.getParameter("choice"));
			int s_id3 = Integer.parseInt(request.getParameter("t_id"));
			int user_id3 = Integer.parseInt(request.getParameter("user_id"));
			SelectImp sImp2 = new SelectImp();
			boolean flag2 = sImp2.Collect(user_id3, s_id3, choice2);
			System.out.println(flag2);
			if (flag2) {
				printWriter.write("已取消收藏");
			} else {
				printWriter.write("取消收藏失败");
			}
			printWriter.close();
			break;
		default:
			break;
		}
	}
}
